﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.ComponentModel;
using System.Collections.ObjectModel;
using ThreeByte.ActiveDeck.AddIn.Content.Controls;

namespace ThreeByte.ActiveDeck.Config
{
    public class UserSlideConfig : PageConfig
    {

        #region Public Properties

        private GraphicResource _image;
        public GraphicResource Image {
            get {
                return _image;
            }
            set {
                _image = value;
                NotifyPropertyChanged("Image");
            }
        }

        private bool _isSnapshot;
        public bool IsSnapshot {
            get {
                return _isSnapshot;
            }
            set {
                _isSnapshot = value;
                NotifyPropertyChanged("IsSnapshot");
            }
        }

        private bool _scaleToFill;
        public bool ScaleToFill {
            get {
                return _scaleToFill;
            }
            set {
                _scaleToFill = value;
                NotifyPropertyChanged("ScaleToFill");
            }
        }

        private bool _zoomEnabled;
        public bool ZoomEnabled {
            get {
                return _zoomEnabled;
            }
            set {
                _zoomEnabled = value;
                NotifyPropertyChanged("ZoomEnabled");
            }
        }

        private bool _navigateForward;
        public bool NavigateForward {
            get {
                return _navigateForward;
            }
            set {
                _navigateForward = value;
                NotifyPropertyChanged("NavigateForward");
            }
        }

        private bool _navigateBackward;
        public bool NavigateBackward {
            get {
                return _navigateBackward;
            }
            set {
                _navigateBackward = value;
                NotifyPropertyChanged("NavigateBackward");
            }
        }
        #endregion Public Properties


        public UserSlideConfig() {

        }

        public UserSlideConfig(XElement node, Dictionary<Guid, GraphicResource> resources)
            : base(node, resources) {

            XElement graphic = node.Element(ns + "Graphic");
            if(graphic != null) {
                Guid resId = Guid.Parse(graphic.Attribute("ID").Value);
                Image = resources[resId];
                if(graphic.Attribute("IsSnapshot") != null) {
                    IsSnapshot = bool.Parse(graphic.Attribute("IsSnapshot").Value);
                }
                if(graphic.Attribute("ScaleToFill") != null) {
                    ScaleToFill = bool.Parse(graphic.Attribute("ScaleToFill").Value);
                }
            }
            if(node.Attribute("ZoomEnabled") != null) {
                ZoomEnabled = bool.Parse(node.Attribute("ZoomEnabled").Value);
            }

            XElement navNode = node.Element(ns + "SwipeToNavigate");
            if(navNode != null) {
                NavigateForward = bool.Parse(navNode.Attribute("Forward").Value);
                NavigateBackward = bool.Parse(navNode.Attribute("Backward").Value);
            }
        }

        public override XElement ToXml() {
            XElement node = base.ToXml();

            if(Image != null) {
                XElement graphicNode = Image.GetXmlHeader();
                if(IsSnapshot) {
                    graphicNode.Add(new XAttribute("IsSnapshot", IsSnapshot));
                }
                if(ScaleToFill) {
                    graphicNode.Add(new XAttribute("ScaleToFill", ScaleToFill));
                }
                node.Add(graphicNode);
            }
            node.Add(new XAttribute("ZoomEnabled", ZoomEnabled));
            XElement navNode = new XElement(ns + "SwipeToNavigate");
            navNode.Add(new XAttribute("Forward", NavigateForward));
            navNode.Add(new XAttribute("Backward", NavigateBackward));
            node.Add(navNode);

            return node;
        }

        public override List<GraphicResource> GetPersistentGraphicResources() {
            List<GraphicResource> imageList = new List<GraphicResource>();
            if(Image != null) {
                imageList.Add(Image);
            }
            return imageList;
        }
    }
}
